//
//  AppDelegate.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/8.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //设置UIBarButtonItem的整体颜色
        setupApperence()
        
        //打开数据库
        YCLSQLiteManager.sharedSQLiteManager
//
//        let tabBarController = YCLTabBarController()
//        //设置窗口的根控制器
        window?.rootViewController = defaultViewController()

        //让窗口可见
        window?.makeKeyAndVisible()
        
        
        return true
    }
    
    //控制器的切换
    
    func switchViewController(controller: UIViewController) {
    
        window?.rootViewController = controller
    }
    
    
    private func defaultViewController() -> UIViewController {
        
        //判断用户是否登录
        if !YCLUserAccountViewModel.sharedUserAccountViewModel.isLogin {
            //未登录
            return YCLTabBarController()
        }else{
            
            //登录
            //判断是否为新版本
            let newVersion = isNewVersion()
            
            if newVersion {
                
                return YCLNewFeatureController()
            }else{
                
                return YCLWelcomeViewController()
            }
            
        }
    }

    
    
    //判断是否为新版本
    private func isNewVersion() -> Bool {
    
        //获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as? String
        
        //比较上次保存的版本与当前版本的大小
        let savedVersion = NSUserDefaults.standardUserDefaults().stringForKey("savedVersion")
        
        if currentVersion != savedVersion {
            
            //保存到偏好设置中
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "savedVersion")
            //立即同步
            NSUserDefaults.standardUserDefaults().synchronize()
            
            return true
        }
        
        return false
   
    }
    
    //设置全局UIBarButtonItem颜色
    private func setupApperence(){
    
        UIBarButtonItem.appearance().tintColor = UIColor.orangeColor()
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
        YCLStatusDAL.sharedStatusDAL.deleteCacheStatus()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

